<?php
/**
 * Groo ISBNTest Class.
 *
 * $Horde: incubator/groo/lib/ISBNTest.php,v 1.3 2006/08/16 03:17:16 luciano Exp $
 *
 * Copyright 2004-2006 Luciano Ramalho <luciano@ramalho.org>
 *
 * See the enclosed file COPYING for license information (GPL). If you
 * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
 *
 * @author  Luciano Ramalho <luciano@ramalho.org>
 * @package Groo
 */

// Call ISBNTest::main() if this source file is executed directly.
if (!defined("PHPUnit2_MAIN_METHOD")) {
    define("PHPUnit2_MAIN_METHOD", "ISBNTest::main");
}

require_once "PHPUnit2/Framework/TestCase.php";
require_once "PHPUnit2/Framework/TestSuite.php";

// You may remove the following line when all tests have been implemented.
require_once "PHPUnit2/Framework/IncompleteTestError.php";

require_once "ISBN.php";

class ISBNTest extends PHPUnit2_Framework_TestCase {

    public static function main() {
        require_once "PHPUnit2/TextUI/TestRunner.php";

        $suite  = new PHPUnit2_Framework_TestSuite("ISBNTest");
        $result = PHPUnit2_TextUI_TestRunner::run($suite);
    }

    protected function setUp() {
        $this->digits10ok0 = '0596002920';
        $this->digits10ok2 = ' 853-521-714-2 ';
        $this->digits10ok9 = '0-3162-8929-9';
        $this->digits10okX = '013147149X';
        $this->digits10nok = '0131471490';
        $this->digits13ok0 = '9780316289290';
        $this->digits13ok8 = '978-85-352-1714-8';
        $this->digits13nok = '9780596100679';
        $this->digits13isbn = '9791231231233';
    }

    public function testFilterDigits() {
        $this->assertEquals('1234567890123',ISBN::filterDigits('1234567890123'));
        $this->assertEquals('0596101392',ISBN::filterDigits('\t0 596 10139-2\n'));
        $this->assertEquals('013147149X',ISBN::filterDigits('0-13-147149-X'));
        $this->assertEquals('1234X',ISBN::filterDigits('X1X2X3X4X'));
    }

    public function testIsValidISBN10() {
        $this->assertTrue(ISBN::isValidISBN10($this->digits10ok0));
        $this->assertTrue(ISBN::isValidISBN10($this->digits10ok2));
        $this->assertTrue(ISBN::isValidISBN10($this->digits10ok9));
        $this->assertTrue(ISBN::isValidISBN10($this->digits10okX));
        $this->assertFalse(ISBN::isValidISBN10($this->digits10nok));
        $this->assertFalse(ISBN::isValidISBN10($this->digits13ok0));
    }

    public function testIsValidISBN13() {
        $this->assertTrue(ISBN::isValidISBN13($this->digits13ok0));
        $this->assertTrue(ISBN::isValidISBN13($this->digits13ok8));
        $this->assertFalse(ISBN::isValidISBN13($this->digits13nok));
        $this->assertFalse(ISBN::isValidISBN13($this->digits10ok0));
    }

    public function testIsValidISBN() {
        $this->assertTrue(ISBN::isValidISBN($this->digits10ok0));
        $this->assertTrue(ISBN::isValidISBN($this->digits10ok2));
        $this->assertTrue(ISBN::isValidISBN($this->digits13ok0));
        $this->assertTrue(ISBN::isValidISBN($this->digits13isbn));
        $this->assertFalse(ISBN::isValidISBN($this->digits10nok));
        $this->assertFalse(ISBN::isValidISBN($this->digits13nok));
    }

    public function testHasValidISBN10() {
        $this->assertTrue(ISBN::hasValidISBN10($this->digits13ok0));
        $this->assertTrue(ISBN::hasValidISBN10($this->digits13ok8));
        $this->assertFalse(ISBN::hasValidISBN10($this->digits13isbn));
        $this->assertFalse(ISBN::hasValidISBN10($this->digits10nok));
        $this->assertFalse(ISBN::hasValidISBN10($this->digits13nok));
    }

    public function testConvertEANtoISBN10() {
        //                $this->digits10ok2                   $this->digits13ok8
        $this->assertEquals('8535217142',ISBN::convertISBN13toISBN10('9788535217148'));
    }


}

// Call ISBNTest::main() if this source file is executed directly.
if (PHPUnit2_MAIN_METHOD == "ISBNTest::main") {
    ISBNTest::main();
}
?>
